package com.chaoyong.higo.adapter;


import com.chaoyong.higo.R;
import com.chaoyong.higo.activity.AddReceiptAddressActivity;
import com.chaoyong.higo.base.BaseAdapter;
import com.chaoyong.higo.bean.GetAddressInfoBean;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.utils.ViewHolder;
import com.chaoyong.higo.view.ConfirmDialog;
import com.chaoyong.higo.view.ConfirmDialog.ClickListenerInterface;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 收货地址
 * 
 * @author zk
 *
 */
public class ReceiptAddressAdapter extends BaseAdapter<GetAddressInfoBean.DataEntity> {

	/** 姓名 */
	private TextView adress_adp_name;
	/** 手机号 */
	private TextView adress_adp_phone;
	/** 详细地址 */
	private TextView adress_adp_detail;
	/** 设置默认 */
	private TextView adress_adp_default;
	/** 编辑 */
	private RelativeLayout adress_adp_edit_lay;
	/** 删除 */
	private RelativeLayout adress_adp_delete_lay;
	private RelativeLayout defaultLay;
	
	private Activity context;
	
	private OnDefaultAddressChange addressChangelistener;
	private OnAddressDeleteListener onAddressDeleteListener;

	public ReceiptAddressAdapter(Context context,OnDefaultAddressChange addressChangelistener,OnAddressDeleteListener onAddressDeleteListener) {
		super(context);
		this.context = (Activity) context;
		this.addressChangelistener = addressChangelistener;
		this.onAddressDeleteListener = onAddressDeleteListener;
	}


	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_receipt_address, null);
		}

		findView(convertView);
		
		//地址的自增id
		final String id = mList.get(position).getId();
		//收货人姓名
		String consignee = mList.get(position).getConsignee();
		//详细地址
		String address = mList.get(position).getAddress();
		//手机号
		String phone = mList.get(position).getPhone();
		//是否为默认地址
		String default_address = mList.get(position).getDefault_address();
		
		adress_adp_name.setText(consignee);
		adress_adp_phone.setText(phone);
		adress_adp_detail.setText(address);
		
		
		if (default_address.equals("0") || default_address.equals("")) {
			setDifferenceImg(adress_adp_default, Color.parseColor("#666666"), R.drawable.s_dui1);
		}
		if(default_address.equals("1")){
			setDifferenceImg(adress_adp_default, Color.parseColor("#c31d29"), R.drawable.s_dui2);
		}
		
		// 设置默认地址
		defaultLay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				setDifferenceImg(adress_adp_default, Color.parseColor("#c31d29"), R.drawable.s_dui2);
//				Toast.makeText(mContext, "默认" + id, Toast.LENGTH_SHORT).show();
//				httpSetDefault(uid, id);
				addressChangelistener.onDefaulAdsChange(id);
			}
		});

		/**编辑地址*/
		adress_adp_edit_lay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				editAdress(id,position);
			}
		});

		adress_adp_delete_lay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定删除？", "确定", "取消");
				confirmDialog.show();
				confirmDialog.setCancelable(false);
				confirmDialog.setClicklistener(new ClickListenerInterface() {
					
					@Override
					public void doConfirm() {
						onAddressDeleteListener.onAddressDelete(id);
						
						confirmDialog.dismiss();
						
						
					}
					
					@Override
					public void doCancel() {
						// TODO Auto-generated method stub
						confirmDialog.dismiss();
					}
				});
			}
		});

		return convertView;
	}

	private void findView(View convertView) {
		adress_adp_name = ViewHolder.get(convertView, R.id.adress_adp_name);
		adress_adp_phone = ViewHolder.get(convertView, R.id.adress_adp_phone);
		adress_adp_detail = ViewHolder.get(convertView, R.id.adress_adp_detail);
		adress_adp_default = ViewHolder.get(convertView, R.id.adress_adp_default);
		adress_adp_edit_lay = ViewHolder.get(convertView, R.id.adress_adp_edit_lay);
		adress_adp_delete_lay = ViewHolder.get(convertView, R.id.adress_adp_delete_lay);
		defaultLay = ViewHolder.get(convertView, R.id.defaultLay);
	}

	private void setDifferenceImg(TextView tv, int color, int resId) {
		tv.setTextColor(color);

		Drawable drawable = tv.getResources().getDrawable(resId);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
		tv.setCompoundDrawables(drawable, null, null, null);
	}
	
	
	
	
	public interface OnDefaultAddressChange{
		void onDefaulAdsChange(String adsId);
	}
	
	public interface OnAddressDeleteListener{
		void onAddressDelete(String adsId);
	}
		
	

	private void editAdress(String id,int pos) {
		 Intent intent = new Intent(mContext, AddReceiptAddressActivity.class);
		 intent.putExtra("id", mList.get(pos).getId());
		 intent.putExtra("name", mList.get(pos).getConsignee());
		 intent.putExtra("phone",  mList.get(pos).getPhone());
		 intent.putExtra("isEdit", true);
		 mContext.startActivity(intent);
	}
}
